//
//  NSString+Extension.m
//  百利天下
//
//  Created by cyz on 16/5/16.
//  Copyright © 2016年 BL. All rights reserved.
//

#import "NSString+Extension.h"
// 引入必要的头文件
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import "NSDate+Utilities.h"
#import <sys/utsname.h>
@implementation NSString (Extension)

#pragma mark -- 类方法
/** 一般加密 */
+ (NSString *)commonMD5String:(NSString *)str{
//    const char *myPWD = [str UTF8String];
//    unsigned char mdc[16];
//    CC_MD5(myPWD, (CC_LONG)strlen(myPWD), mdc);
//    NSMutableString *md5String = [NSMutableString string];
//    for (int i = 0; i < 16; i++) {
//        [md5String appendFormat:@"%02x",mdc[i]];
//    }
//    return md5String;
    return [str commonMD5String];
}

/**  加强解密难度  */
+ (NSString *)strengthenMD5StringWith:(NSString *)str{
//    const char *myPWD = [str UTF8String];
//    unsigned char mdc[16];
//    CC_MD5(myPWD, (CC_LONG)strlen(myPWD), mdc);
//    NSMutableString *md5String = [NSMutableString string];
//    [md5String appendFormat:@"%02x",mdc[0]];
//    for (int i = 0; i < 16; i++) {
//        [md5String appendFormat:@"%02x",mdc[i]^mdc[0]];
//    }
//    
//    return md5String;
    return [str strengthenMD5String];
}
//加密
+(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key iv:(NSString *)iv
{
    char keyPtr[kCCKeySizeAES128+1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
      
    NSData* data = [plainText dataUsingEncoding:NSASCIIStringEncoding];
    NSUInteger dataLength = [data length];
      
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          (__bridge const void *)(iv.length > 0? iv: NULL),
                                          [data bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        //return [GTMBase64 stringByEncodingData:resultData];
//        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
//        return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        // 对加密后的数据进行 base64 编码
        
//        NSData *nsdata = [@"iOS Developer Tips encoded in Base64" dataUsingEncoding:NSUTF8StringEncoding];
//        NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
//        return [resultData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    
        
//        NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
//        NSDataBase64Encoding76CharacterLineLength = 1UL << 1,
//
//        // Use zero or more of the following to specify which kind of line ending is inserted. The default line ending is CR LF.
//        NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
//        NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,
        
        NSString *str = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        NSString *str1 = [[NSString alloc] initWithData:resultData encoding:NSASCIIStringEncoding];
        NSString *str2 = [[NSString alloc] initWithData:resultData encoding:NSNonLossyASCIIStringEncoding];
        
        
        
        NSData *base64Data = [resultData base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        NSString *baseString = [[NSString alloc]initWithData:base64Data encoding:NSASCIIStringEncoding
];
        
        NSString *baseString1 = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
        
        NSString *baseString2 = [[NSString alloc]initWithData:base64Data encoding:NSNonLossyASCIIStringEncoding
];
        NSLog(@"str=%@\n str1=%@\n str2=%@\n base64=%@\n baseString1=%@  \n baseString2 = %@",str,str1,str2,baseString,baseString1,baseString2);
        return baseString;
//        return [self hexStringFromData:resultData];
  
    }
    free(buffer);
    return nil;
}


+ (NSString *)base64Encode:(NSString *)data{
    if (!data) {
        return nil;
    }
    NSData *sData = [data dataUsingEncoding:NSUTF8StringEncoding];
    NSData *base64Data = [sData base64EncodedDataWithOptions:0];
    NSString *baseString = [[NSString alloc]initWithData:base64Data encoding:NSUTF8StringEncoding];
    return baseString;
}

+ (NSString *)base64Dencode:(NSString *)data{
    if (!data) {
        return nil;
    }
    NSData *sData = [[NSData alloc]initWithBase64EncodedString:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSString *dataString = [[NSString alloc]initWithData:sData encoding:NSUTF8StringEncoding];
    return dataString;
}


// 普通字符串转换为十六进
+ (NSString *)hexStringFromData:(NSData *)data {
    Byte *bytes = (Byte *)[data bytes];
    // 下面是Byte 转换为16进制。
    NSString *hexStr = @"";
    for(int i=0; i<[data length]; i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i] & 0xff]; //16进制数
        newHexStr = [newHexStr uppercaseString];
          
        if([newHexStr length] == 1) {
            newHexStr = [NSString stringWithFormat:@"0%@",newHexStr];
        }
          
        hexStr = [hexStr stringByAppendingString:newHexStr];
          
    }
    return hexStr;
}
/**
 *  双重MD5加密
 *
 *  @param NSString 需要加密的字符串
 *
 *  @return 加密完成返回的字符串
 */
+ (NSString *)doubleMD5String:(NSString *)str
{
    
    return  [str doubleMD5String];
}

#pragma mark -- 对象方法
/** 一般加密 */
- (NSString *)commonMD5String
{
    const char *myPWD = [self UTF8String];
    unsigned char mdc[16];
    CC_MD5(myPWD, (CC_LONG)strlen(myPWD), mdc);
    NSMutableString *md5String = [NSMutableString string];
    for (int i = 0; i < 16; i++) {
        [md5String appendFormat:@"%02x",mdc[i]];
    }
    return md5String;
}
/**
 *  双重MD5加密
 *
 *  @return 加密完成的字符串
 */
- (NSString *)doubleMD5String
{
    return [[self commonMD5String] commonMD5String];
}

/**
 *  MD5加密
 *
 *  @return 加密完成的字符串
 */
- (NSString *)singleMD5String
{
    return [self commonMD5String];
}


/** 加强解密难度 */
- (NSString *)strengthenMD5String
{
    const char *myPWD = [self UTF8String];
    unsigned char mdc[16];
    CC_MD5(myPWD, (CC_LONG)strlen(myPWD), mdc);
    NSMutableString *md5String = [NSMutableString string];
    [md5String appendFormat:@"%02x",mdc[0]];
    for (int i = 0; i < 16; i++) {
        [md5String appendFormat:@"%02x",mdc[i]^mdc[0]];
    }
    return md5String;
}

#pragma mark - 把URL中的参数转换成字典
- (NSMutableDictionary *)changeParamIntoDic
{
   // 获取URL问号的位置，问号后面是参数列表
    if (![self containsString:@"?"]) {
        return [NSMutableDictionary dictionary];
    }
    NSRange range = [self rangeOfString:@"?"];
    // 获取参数列表
    NSString *param = [self substringFromIndex:range.location+1];
    
    // 对键值对进行拆分  分隔符为 &
    NSArray<NSString *> *subArray = [param componentsSeparatedByString:@"&"];
    
    // 把subArray转换成字典
    // dictionary 中存放着一个URL中转换的键值对
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    // 遍历数组
    [subArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        // 通过=拆分键和值
        NSArray<NSString*> *dicArray = [obj componentsSeparatedByString:@"="];
        // 给字典加入元素
        [dictionary setObject:dicArray[1] forKey:dicArray[0]];
    }];
    
    return dictionary;
}

+ (NSString *)getDeviceInfo {
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    // 获取设备标识Identifier
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];

    //iPhone
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus (A1897)";
    if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";
    if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    if ([platform isEqualToString:@"iPhone12,1"]) return @"iPhone 11";
    if ([platform isEqualToString:@"iPhone12,3"]) return @"iPhone 11 Pro";
    if ([platform isEqualToString:@"iPhone12,5"]) return @"iPhone 11 Pro Max";
    if ([platform isEqualToString:@"iPhone12,8"]) return @"iPhone SE (2nd generation)";
    if ([platform isEqualToString:@"iPhone13,1"]) return @"iPhone 12 mini";
    if ([platform isEqualToString:@"iPhone13,2"]) return @"iPhone 12";
    if ([platform isEqualToString:@"iPhone13,3"]) return @"iPhone 12 Pro";
    if ([platform isEqualToString:@"iPhone13,4"]) return @"iPhone 12 Pro Max";
    
    if ([platform isEqualToString:@"iPhone14,4"])    return @"iPhone 13 mini";
    if ([platform isEqualToString:@"iPhone14,5"])    return @"iPhone 13";
    if ([platform isEqualToString:@"iPhone14,2"])    return @"iPhone 13 Pro";
    if ([platform isEqualToString:@"iPhone14,3"])    return @"iPhone 13 Pro Max";
    
    if ([platform isEqualToString:@"iPhone14,6"])    return @"iPhone SE (3nd generation)"; //(2nd generation)
    if ([platform isEqualToString:@"iPhone14,7"])    return @"iPhone 14";
    if ([platform isEqualToString:@"iPhone14,8"])    return @"iPhone 14 Plus";
    if ([platform isEqualToString:@"iPhone15,2"])    return @"iPhone 14 Pro";
    if ([platform isEqualToString:@"iPhone15,3"])    return @"iPhone 14 Pro Max";

    //iPad
    if ([platform isEqualToString:@"iPad1,1"])   return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])   return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,2"])   return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])   return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,4"])   return @"iPad 2";
    if ([platform isEqualToString:@"iPad3,1"])   return @"iPad (3rd generation)";
    if ([platform isEqualToString:@"iPad3,2"])   return @"iPad (3rd generation)";
    if ([platform isEqualToString:@"iPad3,3"])   return @"iPad (3rd generation)";
    if ([platform isEqualToString:@"iPad3,4"])   return @"iPad (4th generation)";
    if ([platform isEqualToString:@"iPad3,5"])   return @"iPad (4th generation)";
    if ([platform isEqualToString:@"iPad3,6"])   return @"iPad (4th generation)";
    if ([platform isEqualToString:@"iPad6,11"])  return @"iPad (5th generation)";
    if ([platform isEqualToString:@"iPad6,12"])  return @"iPad (5th generation)";
    if ([platform isEqualToString:@"iPad7,5"])   return @"iPad (6th generation)";
    if ([platform isEqualToString:@"iPad7,6"])   return @"iPad (6th generation)";
    if ([platform isEqualToString:@"iPad7,11"])  return @"iPad (7th generation)";
    if ([platform isEqualToString:@"iPad7,12"])  return @"iPad (7th generation)";
    if ([platform isEqualToString:@"iPad11,6"])  return @"iPad (8th generation)";
    if ([platform isEqualToString:@"iPad11,7"])  return @"iPad (8th generation)";


    //iPad Air
    if ([platform isEqualToString:@"iPad4,1"])   return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,2"])   return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,3"])   return @"iPad Air";
    if ([platform isEqualToString:@"iPad5,3"])   return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad5,4"])   return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad11,3"])  return @"iPad Air (3rd generation)";
    if ([platform isEqualToString:@"iPad11,4"])  return @"iPad Air (3rd generation)";
    if ([platform isEqualToString:@"iPad13,1"])  return @"iPad Air (4th generation)";
    if ([platform isEqualToString:@"iPad13,2"])  return @"iPad Air (4th generation)";

    //iPad mini
    if ([platform isEqualToString:@"iPad2,5"])   return @"iPad mini";
    if ([platform isEqualToString:@"iPad2,6"])   return @"iPad mini";
    if ([platform isEqualToString:@"iPad2,7"])   return @"iPad mini 1";
    if ([platform isEqualToString:@"iPad4,4"])   return @"iPad mini 2";
    if ([platform isEqualToString:@"iPad4,5"])   return @"iPad mini 2";
    if ([platform isEqualToString:@"iPad4,6"])   return @"iPad mini 2";
    if ([platform isEqualToString:@"iPad4,7"])   return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad4,8"])   return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad4,9"])   return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad5,1"])   return @"iPad mini 4";
    if ([platform isEqualToString:@"iPad5,2"])   return @"iPad mini 4";
    if ([platform isEqualToString:@"iPad11,1"])  return @"iPad mini 5";
    if ([platform isEqualToString:@"iPad11,2"])  return @"iPad mini 5";

    // iPad Pro
    if ([platform isEqualToString:@"iPad6,3"])    return @"iPad Pro (9.7-inch)";
    if ([platform isEqualToString:@"iPad6,4"])    return @"iPad Pro (9.7-inch)";
    if ([platform isEqualToString:@"iPad6,7"])    return @"iPad Pro (12.9-inch)";
    if ([platform isEqualToString:@"iPad6,8"])    return @"iPad Pro (12.9-inch)";
    if ([platform isEqualToString:@"iPad7,1"])    return @"iPad Pro (12.9-inch) (2nd generation)";
    if ([platform isEqualToString:@"iPad7,2"])    return @"iPad Pro (12.9-inch) (2nd generation)";
    if ([platform isEqualToString:@"iPad7,3"])  return @"iPad Pro (10.5 inch)";
    if ([platform isEqualToString:@"iPad7,4"])  return @"iPad Pro (10.5-inch)";
    if ([platform isEqualToString:@"iPad8,1"])  return @"iPad Pro (11-inch)";
    if ([platform isEqualToString:@"iPad8,2"])  return @"iPad Pro (11-inch)";
    if ([platform isEqualToString:@"iPad8,3"])  return @"iPad Pro (11-inch)";
    if ([platform isEqualToString:@"iPad8,4"])  return @"iPad Pro (11-inch)";
    if ([platform isEqualToString:@"iPad8,5"])  return @"iPad Pro (12.9-inch) (3rd generation)";
    if ([platform isEqualToString:@"iPad8,6"])  return @"iPad Pro (12.9-inch) (3rd generation)";
    if ([platform isEqualToString:@"iPad8,7"])  return @"iPad Pro (12.9-inch) (3rd generation)";
    if ([platform isEqualToString:@"iPad8,8"])  return @"iPad Pro (12.9-inch) (3rd generation)";
    if ([platform isEqualToString:@"iPad8,9"])  return @"iPad Pro (11-inch) (2nd generation)";
    if ([platform isEqualToString:@"iPad8,10"])  return @"iPad Pro (11-inch) (2nd generation)";
    if ([platform isEqualToString:@"iPad8,11"])  return @"iPad Pro (12.9-inch) (4th generation)";
    if ([platform isEqualToString:@"iPad8,12"])  return @"iPad Pro (12.9-inch) (4th generation)";

    //iPod Touch
    if ([platform isEqualToString:@"iPod1,1"])   return @"iPod Touch";
    if ([platform isEqualToString:@"iPod2,1"])   return @"iPod Touch 2";
    if ([platform isEqualToString:@"iPod3,1"])   return @"iPod Touch 3";
    if ([platform isEqualToString:@"iPod4,1"])   return @"iPod Touch 4)";
    if ([platform isEqualToString:@"iPod5,1"])   return @"iPod Touch 5";
    if ([platform isEqualToString:@"iPod7,1"])   return @"iPod Touch 6";
    if ([platform isEqualToString:@"iPod9,1"])   return @"iPod Touch 7";

    // Apple Watch
    if ([platform isEqualToString:@"Watch1,1"])   return @"Apple Watch";
    if ([platform isEqualToString:@"Watch1,2"])   return @"Apple Watch";
    if ([platform isEqualToString:@"Watch2,6"])   return @"Apple Watch Series 1";
    if ([platform isEqualToString:@"Watch2,7"])   return @"Apple Watch Series 1";
    if ([platform isEqualToString:@"Watch2,3"])   return @"Apple Watch Series 2";
    if ([platform isEqualToString:@"Watch2,4"])   return @"Apple Watch Series 2";
    if ([platform isEqualToString:@"Watch3,1"])   return @"Apple Watch Series 3";
    if ([platform isEqualToString:@"Watch3,2"])   return @"Apple Watch Series 3";
    if ([platform isEqualToString:@"Watch3,3"])   return @"Apple Watch Series 3";
    if ([platform isEqualToString:@"Watch3,4"])   return @"Apple Watch Series 3";

    // 模拟器
    if ([platform isEqualToString:@"i386"])   return @"iPhone Simulator";
    if ([platform isEqualToString:@"x86_64"])  return @"iPhone Simulator";

    return platform;
}

+ (NSInteger)compareVersion:(NSString *)version1 toVersion:(NSString *)version2 {
    NSArray *list1 = [version1 componentsSeparatedByString:@"."];
    NSArray *list2 = [version2 componentsSeparatedByString:@"."];
    for (int i = 0; i < list1.count || i < list2.count; i++)
    {
        NSInteger a = 0, b = 0;
        if (i < list1.count) {
            a = [list1[i] integerValue];
        }
        if (i < list2.count) {
            b = [list2[i] integerValue];
        }
        if (a > b) {
            return 1;//version1大于version2
        } else if (a < b) {
            return -1;//version1小于version2
        }
    }
    return 0;//version1等于version2

}

@end
